//Table D1: Summary statistics by income quintiles and geographic area

*===============================================================================
// Step 1: Create dataset - reduce to couples and create heterogeneity variables
*===============================================================================
use    "${dataout}MainDataset" , clear
keep if year>=2015 & year<=2017
keep if couple==1

// The file "Link_grkrets_regtype1" maps basic statistical units to aggregated region types.
// Region types are merged to basic statistical unit of residence.
sort grkrets
merge m:1 grkrets using  "${datain}Link_grkrets_regtype1", keep(match master)
drop _merge

gen CityCategory=. // Creating four categories based on region types
replace CityCategory=1 if (regtype1==1 | regtype1==3) & !missing(regtype1) // Oslo, Bergen, Trondheim or Stavanger
replace CityCategory=2 if (regtype1==2 | regtype1==4) & !missing(regtype1) // Suburbs of the four largest cities
replace CityCategory=3 if (regtype1==5 | regtype1==6) & !missing(regtype1) // Other, smaller cities
replace CityCategory=4 if (regtype1==7) & !missing(regtype1) // Rural areas

// Merging region types to basic statistical unit of the workplace.
forvalues i=1/2 {
	preserve
		use "${datain}Link_grkrets_regtype1" , clear
		rename grkrets grk_bed`i'
		rename regtype1 regtype1_bed`i'
		tempfile helpfile_`i'
		save `helpfile_`i''
	restore
	sort grk_bed`i'
	merge m:1 grk_bed`i' using  `helpfile_`i'', keep(match master)
	drop _merge
}

forvalues i=1/2 {
	gen CityCategory_bed`i' = . // Creating four categories based on region types (workplace of each spouse)
	replace CityCategory_bed`i'=1 if (regtype1_bed`i'==1 | regtype1_bed`i'==3) & !missing(regtype1_bed`i')
	replace CityCategory_bed`i'=2 if (regtype1_bed`i'==2 | regtype1_bed`i'==4) & !missing(regtype1_bed`i')
	replace CityCategory_bed`i'=3 if (regtype1_bed`i'==5 | regtype1_bed`i'==6) & !missing(regtype1_bed`i')
	replace CityCategory_bed`i'=4 if (regtype1_bed`i'==7) & !missing(regtype1_bed`i')
}

* === Interaction variable for geography =======================================
gen commuting_dir = .
replace commuting_dir = 1 if CityCategory == 1 													 // Within city
replace commuting_dir = 2 if CityCategory == 1 & CityCategory_bed1 != 1 & CityCategory_bed1 != . // From city
replace commuting_dir = 2 if CityCategory == 1 & CityCategory_bed2 != 1 & CityCategory_bed2 != . // From city
replace commuting_dir = 3 if CityCategory == 2 													 // Within suburb
replace commuting_dir = 4 if CityCategory == 2 & CityCategory_bed1 == 1 						 // Suburb to city
replace commuting_dir = 4 if CityCategory == 2 & CityCategory_bed2 == 1 						 // Suburb to city
replace commuting_dir = 5 if CityCategory == 3 													 // Other cities
replace commuting_dir = 6 if CityCategory == 4 													 // Rural areas

* === Interaction variable for income ==========================================
egen wies_fam_sum =rowtotal(wies1 wies2), missing
xtile wies_fam_sum_quintile =  wies_fam_sum, nq(5)
compress

* === Variables for heterogeneous effects ======================================
forvalues i = 1/5 { // Re-create heterogeneous treatment variables
gen tr1_inc_q`i' = toll_fam_mean   * (wies_fam_sum_quintile == `i')
gen tr2_inc_q`i' = ptl_fam_km_mean * (wies_fam_sum_quintile == `i')
}
forvalues i = 1/6 { // Re-create heterogeneous treatment variables
gen tr1_geo`i' = toll_fam_mean   * (commuting_dir == `i')	
gen tr2_geo`i' = ptl_fam_km_mean * (commuting_dir == `i')	
}

* ==============================================================================
* STEP 2: Making table
*===============================================================================
eststo clear

/* === BY INCOME ============================================================ */
* Choose variables 
local varlist BEV_fam_yes car_fam_yes ICE_fam_count car_fam_count toll_fam_mean ptl_fam_km_mean
* For each income group: 
forvalues income_group = 1(1)5 {
	* Mark estimation sample
	estimates esample: BEV_fam_yes toll_fam_mean ptl_fam_km_mean tr1_inc_q1 tr1_inc_q2 tr1_inc_q3 tr1_inc_q4 tr2_inc_q1 tr2_inc_q2 tr2_inc_q3 tr2_inc_q4 tr1_geo2 tr1_geo3 tr1_geo4 tr1_geo5 tr1_geo6 tr2_geo2 tr2_geo3 tr2_geo4 tr2_geo5 tr2_geo6 year age1 age2 dist1 dist2 time1 time2 PublicTransitTime_1 PublicTransitTime_2 VENTETID_1  BOARDINGS_1 TAKST_1 VENTETID_2 BOARDINGS_2 TAKST_2 grkrets_num grk_bed1_num grk_bed2_num children antpers_i_regstat_famnr secondhome wies1_decile wies2_decile wealth1_decile wealth2_decile employed1 employed2 retired1 retired2 grputd1 grputd2, replace
	* Store summary statistics per income group
	eststo inc_group_`income_group':  estpost summarize `varlist' if e(sample) == 1 & wies_fam_sum_quintile == `income_group'
}

* Export table to .tex
esttab * using  "${tables}TableD1fragmentA.tex" ///
	, cells(mean(fmt(%12.4fc))) ///
	coeflabels( ///
		BEV_fam_yes "BEV (yes = 1)" ///
		car_fam_yes "Car (yes = 1)" ///
		ICE_fam_count "Number of ICEVs" ///
		car_fam_count "Number of cars" ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///		
	) stats(N, label("Observations") fmt(%9.0fc)) ///
	fragment nonumbers collabels(none) nolines nodepvars nogaps nonotes nomtitle prehead("") posthead("") ///
	replace

/* === BY AREA ============================================================== */	
eststo clear
* Choose variables
local varlist BEV_fam_yes car_fam_yes ICE_fam_count car_fam_count toll_fam_mean ptl_fam_km_mean 
* For each geography group:
forvalues area = 1(1)6 {
	* Mark estimation sample
	estimates esample: BEV_fam_yes toll_fam_mean ptl_fam_km_mean tr1_inc_q1 tr1_inc_q2 tr1_inc_q3 tr1_inc_q4 tr2_inc_q1 tr2_inc_q2 tr2_inc_q3 tr2_inc_q4 tr1_geo2 tr1_geo3 tr1_geo4 tr1_geo5 tr1_geo6 tr2_geo2 tr2_geo3 tr2_geo4 tr2_geo5 tr2_geo6 year age1 age2 dist1 dist2 time1 time2 PublicTransitTime_1 PublicTransitTime_2 VENTETID_1  BOARDINGS_1 TAKST_1 VENTETID_2 BOARDINGS_2 TAKST_2 grkrets_num grk_bed1_num grk_bed2_num children antpers_i_regstat_famnr secondhome wies1_decile wies2_decile wealth1_decile wealth2_decile employed1 employed2 retired1 retired2 grputd1 grputd2, replace
	* Store summary statistics per group
	eststo commuting_dir_`area':  estpost summarize `varlist' if e(sample) == 1 & commuting_dir == `area'
}

* Export table to .tex
esttab * using  "${tables}TableD1fragmentB.tex" ///
	, cells(mean(fmt(%12.4fc))) ///
	coeflabels( ///
		BEV_fam_yes "BEV (yes = 1)" ///
		car_fam_yes "Car (yes = 1)" ///
		ICE_fam_count "Number of ICEVs" ///
		car_fam_count "Number of cars" ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///		
	) stats(N, label("Observations") fmt(%9.0fc)) ///
	fragment nonumbers collabels(none) nolines nodepvars nogaps nonotes nomtitle prehead("") posthead("") ///
	replace


